package edu.upc.autopartescybershop.repository.hibernate;

import edu.upc.autopartescybershop.domain.Modelo;
import edu.upc.autopartescybershop.repository.ModeloRepository;
import java.util.List;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.stereotype.Repository;

@Repository
public class ModeloRepositoryHibernate extends HibernateDaoSupport implements ModeloRepository
{

    @Autowired
    public ModeloRepositoryHibernate(SessionFactory sessionFactory)
    {
        setSessionFactory(sessionFactory);
    }

    @Override
    public List<Modelo> obtenerModelos()
    {
        return getHibernateTemplate().find("from Modelo");
    }

    @Override
    public Modelo obtenerModelo(int codigo)
    {
        return (Modelo) getSession().
                createQuery("from Modelo m"
                + "  where m.Codigo = ?").
                setInteger(0, codigo).uniqueResult();
    }

    @Override
    public void grabarModelo(Modelo modelo)
    {
        getHibernateTemplate().saveOrUpdate(modelo);
    }

    @Override
    public void eliminarModelo(int codigo)
    {
        Modelo obj = obtenerModelo(codigo);
        getHibernateTemplate().delete(obj);
    }
}
